//
// Created by 李传智 on 2018/4/14.
//

#include "IAudioPlay.h"

void IAudioPlay::update(VideoData data) {
    while (!isExist) {
        frameMutex.lock();
        if (frames.size() < maxFrameSize) {
            frames.push_back(data);
            frameMutex.unlock();
            break;
        }
        frameMutex.unlock();
        sleep(1);
    }
}

VideoData IAudioPlay::getVideoData() {
    isRunning = true;
    VideoData data;
    while (!isExist) {
        if (isPaused()) {
            sleep(2);
            continue;
        }
        frameMutex.lock();
        if (!frames.empty()) {
            data = frames.front();
            frames.pop_front();
            frameMutex.unlock();
            pts = data.pts;
            return data;
        }
        frameMutex.unlock();
        sleep(1);
    }
    isRunning = false;
    return data;
}

void IAudioPlay::clear() {
    frameMutex.lock();
    while (!frames.empty()) {
        frames.front().free();
        frames.pop_front();
    }
    pts = 0;
    frameMutex.unlock();
}